import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class RunTask
{
    private static final String serverURL = "https://reserve-hk.appleonline.net/reservation/create";

    private static final String HTTP_GET = "GET";
    
    private static final String HTTP_POST = "POST";
    
    private HttpClient getHttpClient(String type)
    {
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            /*
            KeyStore trustStore = KeyStore.getInstance(KeyStore
                    .getDefaultType());
            File file = new File("cacerts");
            if (HTTP_POST.equals(type))
            {
                file = new File("bcacerts");
            }
            FileInputStream instream = new FileInputStream(file);
            // 密匙库的密码
            trustStore.load(instream, "123456".toCharArray());
            // 注册密匙库
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
            // 不校验域名
            socketFactory
                    .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            int port = 443;
            Scheme sch = new Scheme("https", port, socketFactory);
            if (HTTP_POST.equals(type))
            {
	            httpclient.getConnectionManager().getSchemeRegistry().register(sch);
            }
            */
            return httpclient;
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        return null;
    }
    
    public static void book(Map<String, String> postData, String governmentId)
    {
        RunTask runTask = new RunTask();
        String response = runTask.getResponse(serverURL, HTTP_POST, postData, governmentId);
        System.out.println(response);
    }
    
    private String getResponse(String url, String type, Map<String, String> params, String governmentId)
    {
        try
        {
            if (HTTP_GET.equals(type))
            {
                HttpGet http = new HttpGet(url);
                return parseResponse(getHttpClient(HTTP_GET).execute(http));
            }
            else
            {
                HttpPost http = new HttpPost(url);
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("key", params.get("key")));
                nvps.add(new BasicNameValuePair("payload", params.get("payload")));
                /*
                Set<String> keySet = params.keySet();
                for (String key : keySet)
                {
                    nvps.add(new BasicNameValuePair(key, params.get(key)));
                }
                */
                http.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                String result = parseResponse(getHttpClient(HTTP_POST).execute(http));
                LogUtil.getInstance().writeLog(result + ":" + governmentId);
                System.out.println("post:" + result);
                if ("Failure".equals(result))
                {
                    result = getResponse(url, type, params, governmentId);
                }
                return result;
            }
        }
        catch (ClientProtocolException e)
        {
            System.out.println(e);
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return "";
    }

    private String parseResponse(HttpResponse response)
    {
        try
        {
            InputStream is = response.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null)
            {
                sb.append(line);
            }
            return sb.toString();
        }
        catch (IllegalStateException e)
        {
            System.out.println(e);
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return "";
    }
}
